﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Xml.Serialization;

namespace Istacee.Personal.Desktop.TimeWatcher
{
    /// <summary>
    /// That's freaking ugly... i know...
    /// </summary>
    public static class TaskSerializer
    {
        private static string FilePath = "date.sav";

        public static void Save(List<TrackedTask> tasks)
        {
            FileStream serializer = null; 
            try
            {
                //if(File.Exists(FilePath))
                //    File.Delete(FilePath);
                serializer = new FileStream(FilePath, FileMode.Create);
                XmlSerializer ser = new XmlSerializer(typeof(List<SerializableTrackedTask>));
                ser.Serialize(serializer, tasks.ConvertAll<SerializableTrackedTask>(Converter));
                serializer.Close();
                serializer.Dispose();
            }
            catch (Exception ex)
            {
                if (serializer != null)
                {
                    serializer.Close();
                    serializer.Dispose();
                }
                MessageBox.Show(String.Format("Unable to save. {0}", ex.Message));
            }
        }

        public static List<TrackedTask> Load()
        {
            FileStream serializer = null;
            try
            {
                serializer = new FileStream(FilePath, FileMode.OpenOrCreate);
                XmlSerializer ser = new XmlSerializer(typeof(List<SerializableTrackedTask>));
                var result = (ser.Deserialize(serializer) as List<SerializableTrackedTask>).ConvertAll<TrackedTask>(Converter);
                serializer.Close();
                serializer.Dispose();
                return result;
            }
            catch (Exception ex)
            {
                MessageBox.Show(String.Format("Error Loading : {0}", ex.Message));
                if (serializer != null)
                {
                    serializer.Close();
                    serializer.Dispose();
                }
                return new List<TrackedTask>();
            }
        }

        private static TrackedTask Converter(SerializableTrackedTask input)
        {
            TrackedTask result = new TrackedTask();
            result.Name = input.Name;
            input.Periods.ForEach(t => result.Periods.Add(new Tuple<DateTime, DateTime>(t[0], t[1])));
            return result;
        }


        private static SerializableTrackedTask Converter(TrackedTask input)
        {
            SerializableTrackedTask result = new SerializableTrackedTask();
            result.Periods = new List<List<DateTime>>();
            input.Periods.ForEach(t => result.Periods.Add(new List<DateTime>() { t.Item1, t.Item2 }));
            if (input.Status)
                result.Periods.Add(new List<DateTime>() { input.lastStartup, DateTime.Now });
            result.Name = input.Name;
            return result;
        }
    }
}
